package com.zhiyou100.dao.Impl;

import com.zhiyou100.JDBCUtil.DBUtil;
import com.zhiyou100.dao.ActivityDao;
import com.zhiyou100.emtity.Activity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.BeanUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-20 22:10
 **/
public class ActivityDaoImpl implements ActivityDao {

    private QueryRunner qur = new QueryRunner(DBUtil.getDataSource());
    @Override
    public List<Activity> queryAll() {
        String sql = "select * from activity";
        try {
            List<Activity> query = qur.query(sql, new BeanListHandler<>(Activity.class));
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void add(Activity activity) {
        String sql = "insert into activity(a_id,a_subject,a_content,a_site,a_cost,a_organizer,a_taketime) values(null,?,?,?,?,?,?)";
        try {
            qur.update(sql,activity.getA_subject(),activity.getA_content(),activity.getA_site(),activity.getA_cost(),activity.getA_organizer(),activity.getA_taketime());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public void delete(int id) {
        String sql = "delete from activity where a_id = ?";
        try {
            qur.update(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public Activity query(int id) {
        String sql = "select * from activity where a_id = ?";
        try {
            Activity query = qur.query(sql, new BeanHandler<>(Activity.class), id);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void update(Activity activity) {
        String sql = "update activity set a_subject=?,a_content=?,a_site=?,a_cost=?,a_organizer=?,a_taketime=? where a_id=?";
        try {
            qur.update(sql,activity.getA_subject(),activity.getA_content(),activity.getA_site(),activity.getA_cost(),activity.getA_organizer(),activity.getA_taketime(),activity.getA_id());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }
}
